package com.gab.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gab.mapper.TbCompanyMapper;
import com.gab.mapper.TbContactMapper;
import com.gab.pojo.TbCompany;
import com.gab.service.TbCompanyService;
import com.gab.service.TbContactService;
import com.gab.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import  com.gab.pojo.TbContactExample;
import  com.gab.pojo.TbContactExample.Criteria;
import com.gab.pojo.TbContact;

@Service
public class TbContactServiceImpl  implements TbContactService{
	@Autowired
	private TbContactMapper tbContactMapper;
	
	public List<TbContact> findAll() {
		// TODO Auto-generated method stub
		return tbContactMapper.selectByExample(null);
	}

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbContact> page=   (Page<TbContact>) tbContactMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	public void add(TbContact tbContact) {
		tbContactMapper.insert(tbContact);
		
	}

	public void update(TbContact tbContact) {
		tbContactMapper.updateByPrimaryKey(tbContact);
		
	}

	public TbContact findOne(Long id) {
		
		return tbContactMapper.selectByPrimaryKey(id);
	}

	public void delete(Long[] ids) {
		for(Long id:ids){
			tbContactMapper.deleteByPrimaryKey(id);
		}
		
	}

	public PageResult findPage(TbContact tbContact, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbContactExample example=new TbContactExample();
		Criteria criteria = example.createCriteria();
		
		if(tbContact!=null){			
			if(tbContact.getAddr()!=null && tbContact.getAddr().length()>0){
				criteria.andAddrLike("%"+tbContact.getAddr()+"%");
			}
			if(tbContact.getChannel()!=null && tbContact.getChannel().length()>0){
				criteria.andChannelLike("%"+tbContact.getChannel()+"%");
			}
			if(tbContact.getCustomerservice()!=null && tbContact.getCustomerservice().length()>0){
				criteria.andCustomerserviceLike("%"+tbContact.getCustomerservice()+"%");
			}
			if(tbContact.getEmail()!=null && tbContact.getEmail().length()>0){
				criteria.andEmailLike("%"+tbContact.getEmail()+"%");
			}
			if(tbContact.getPhone()!=null && tbContact.getPhone().length()>0){
				criteria.andPhoneLike("%"+tbContact.getPhone()+"%");
			}
	
		}
		
		Page<TbContact> page= (Page<TbContact>)tbContactMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

}
